package ro.gateway.aida.fnd;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ro.gateway.aida.obj.Activity;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.utils.HttpUtils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FundingByCatEditorServlet.java,v 1.1 2004/10/24 23:37:14 mihaipostelnicu Exp $
 */
public class FundingByCatEditorServlet extends EditorServlet
{
      protected void other_action_performed(
            HttpServletRequest request,
            HttpServletResponse response,
            String action )
            throws IOException, ServletException
      {
      }

      protected void start_editing(
            ServletContext application,
            HttpServletRequest request,
            HttpServletResponse response )
            throws IOException, ServletException
      {
            super.start_editing( application, request, response );

            HttpSession session = request.getSession();
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
            String key = ( String ) bean.get( PNAME_KEY );
            String action = ( String ) bean.get( PNAME_ACTION );

            String box = HttpUtils.getValidTrimedString( request, "box", null );
            if ( box == null )
            {
                  return;
            }
            Object obox = session.getAttribute( box );
            if ( ( obox == null ) || ( !( obox instanceof Hashtable ) ) )
            {
                  return;
            }
            Hashtable zabox = ( Hashtable ) obox;
            bean.put( PNAME_BOX, zabox.get( PNAME_BOX_LIST ) );
            bean.put( PNAME_ACTIVITY_ITEM, zabox.get( PNAME_BOX_ACTIVITY_ITEM ) );

            if ( PV_ACTION_NEW.equals( action ) )
            {
                  FundingByCatItem item = new FundingByCatItem();
                  bean.put( PNAME_BEAN, item );
            }
            else if ( PV_ACTION_EDIT.equals( action ) )
            {
                  ArrayList items = ( ArrayList ) ( ( Hashtable ) obox ).get( PNAME_BOX_LIST );
                  int index = HttpUtils.getInt( request, "index", -1 );
                  if ( index == -1 )
                  {
                        cancel( request, response );
                        return;
                  }

                  FundingByCatItem item = ( FundingByCatItem ) items.get( index );
                  bean.put( PNAME_BEAN, item );
            }
            else
            {
                  cancel( request, response );
            }
      }

      protected void edit(
            HttpServletRequest request,
            HttpServletResponse response )
            throws IOException, ServletException
      {

            request.setAttribute( PNAME_SCREEN, PV_SCR_EDIT );
            request.getRequestDispatcher( JSP_PAGE ).
                  forward( request, response );
      }

      protected void cancel(
            HttpServletRequest request,
            HttpServletResponse response )
            throws IOException, ServletException
      {

            String key = ( String ) request.getAttribute( PNAME_KEY );

            Hashtable beans = ( Hashtable ) request.getSession().getAttribute( getPNAME_BEANS() );
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );

            if ( ( key == null ) || ( bean == null ) )
            {
                  response.sendError( HttpServletResponse.SC_EXPECTATION_FAILED,
                        "bad[::] request" );

                  return;
            }

            beans.remove( key );
            request.setAttribute( PNAME_SCREEN, PV_SCREEN_CANCEL );
            request.getRequestDispatcher( JSP_PAGE ).
                  forward( request, response );
            return;
      }

      protected void save(
            ServletContext application,
            HttpServletRequest request,
            HttpServletResponse response )
            throws IOException, ServletException
      {
      }

      protected void confirm(
            HttpServletRequest request,
            HttpServletResponse response )
            throws IOException, ServletException
      {

            String key = ( String ) request.getAttribute( PNAME_KEY );
            Hashtable bean = ( Hashtable ) request.getAttribute( PNAME_BEAN );
            FundingByCatItem item = ( FundingByCatItem ) bean.get( PNAME_BEAN );

            item.setAmount( HttpUtils.getLong( request, "amount", 0 ) );
            item.setCurrency( HttpUtils.getValidTrimedString( request, "currency", null ) );
            Activity activity = ( Activity ) bean.get( PNAME_ACTIVITY_ITEM );
            int comp_index = HttpUtils.getInt( request, "component_idx", -1 );
            if ( comp_index == -1 )
            {
                  item.setComponent( null );
            }
            else
            {
                  item.setComponent( activity.getComponents()[ comp_index ] );
            }

            String delyitems = HttpUtils.getValidTrimedString( request, "delyitems", null );
            if ( delyitems != null )
            {
                  String[] values = request.getParameterValues( "y_item_idx" );
                  if ( values != null )
                  {
                        java.util.Arrays.sort( values );
                        for ( int i = values.length - 1; i >= 0; i-- )
                        {
                              int value = -1;
                              try
                              {
                                    value = Integer.parseInt( values[ i ] );
                              }
                              catch ( NumberFormatException nfEx )
                              {
                              }

                              if ( value != -1 )
                              {
                                    item.getYearlyItems().remove( value );
                              }
                        }
                  }

                  response.sendRedirect( "efndbycat?action=edit&key=" + key );
                  return;
            }
            String afteryedit = HttpUtils.getValidTrimedString( request, "afteryedit", null );
            if ( afteryedit != null )
            {
                  response.sendRedirect( "efndbycat?action=edit&key=" + key );
                  return;
            }

            if ( !item.isValid() )
            {
                  response.sendRedirect( "efndbycat?err=true&action=edit&key=" + key );
                  return;
            }

            if ( PV_ACTION_NEW.equals( bean.get( PNAME_ACTION ) ) )
            {
                  ArrayList box = ( ArrayList ) bean.get( PNAME_BOX );
                  box.add( item );
            }

            Hashtable beans = ( Hashtable ) request.getSession().getAttribute( getPNAME_BEANS() );
            beans.remove( key );

            request.setAttribute( PNAME_SCREEN, PV_SCREEN_SAVED );
            request.getRequestDispatcher( JSP_PAGE ).
                  forward( request, response );
            return;
      }

      public String getJSP_PAGE()
      {
            return JSP_PAGE;
      }

      public String getPNAME_BEANS()
      {
            return PNAME_BEANS;
      }

      private static final String JSP_PAGE = "/fnd/fnd_by_cat_item_editor.jsp";
      public static final String PNAME_BEANS = "FND_BYCAT_EDITING_ITEMS";
      public static final String PNAME_FUNDING_ITEM = "FUNDING_BY_CAT_ITEM";
      public static final String PNAME_ERRORS = "errors";
      public static final String PNAME_ACTIVITY_ITEM = "item_actitem";

      public static final String PNAME_BOX = "thecbox";
      public static final String PNAME_BOX_LIST = "box_items";
      public static final String PNAME_BOX_ACTIVITY_ITEM = "item_actitem";
      public static final String PV_SCREEN_CANCEL = "SCR_CANCEL";
      public static final String PV_SCREEN_SAVED = "SCR_SAVED";

}
